package com.home.manager.controller;


import com.home.manager.bean.User;
import com.home.manager.common.ManagerError;
import com.home.manager.common.Response;
import com.home.manager.common.Role;
import com.home.manager.service.UserService;
import com.home.manager.utils.ResponseBodyBuilder;
import com.home.manager.utils.UserThreadLocal;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;


@Controller
public class UserManagerController {
    @Resource
    UserService userService;

    @RequestMapping("/user/{resource}")
    @ResponseBody
    public Response<List<String>> resource(@PathVariable("resource") String resource) {
        User currentUser = UserThreadLocal.getCurrentUser();
        String userRole = userService.getUserRole(currentUser.getUserId());
        if (!Role.ADMIN.getName().equals(userRole)) {
            return ResponseBodyBuilder.fail(ManagerError.NOT_ADMIN_ACCESS);
        }
        List<String> byResource = userService.getByResource(resource);
        return ResponseBodyBuilder.ok(byResource);
    }


}
